package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.ActivationRequestParameter;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.slf4j.Logger;

import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class LikeMaterial2 extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private MaterialService materialSer;
    //
    @ActivationRequestParameter
    private String code;
    @ActivationRequestParameter
    private String name;
    @ActivationRequestParameter
    private String spec;
    @ActivationRequestParameter
    private String desc;
    @ActivationRequestParameter
    private String pcb;
    @ActivationRequestParameter
    private String state;
    @ActivationRequestParameter
    private String foot;
    @ActivationRequestParameter
    private String brand;
    @ActivationRequestParameter
    private String supp;//supplier;
    @ActivationRequestParameter
    private String indLv;//industryLevel;
    @ActivationRequestParameter
    private String remark;
    //
    @Property
    private Material material;
    @Property
    private List<Material> list;
    @Property
    private SplitPageInfo page = new SplitPageInfo();

    // init()->initalize->onPassivate->init(xxx)->display
    public Object withInit(Material material, long size, long first) {
        page.setPageSize(size);
        page.setCurrentPage(first);
        //
        this.material = material;
        this.code = material.getCode();
        this.name = material.getName();
        this.spec = material.getSpecification();
        this.desc = material.getDescription();
        this.pcb = material.getPcbDecal();
        this.state = material.getState();
        this.foot = material.getFootprint();
        this.brand = material.getBrand();
        this.supp = material.getSupplier();
        this.indLv = material.getIndustryLevel();
        this.remark = material.getRemark();
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init() {
        checkPermitted(PermissionUtil.MaterialList);
        return null;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long size, long first) {
        checkPermitted(PermissionUtil.MaterialList);
        this.material = MaterialUtil.toMaterial(code, name, spec, desc, pcb, state, foot, brand, supp, indLv, remark);
        page.setPageSize(size);
        page.setCurrentPage(first);
        return null;
    }

    @SetupRender
    Object display() {
        if (material != null) {
            page.setRowCount(materialSer.countLikeByExample(material));
            list = materialSer.findLikeByExample(material, page.getCurrentRow(), page.getPageSize());
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { page.getPageSize(), page.getCurrentPage() };
    }
}
